package cn.zn.modules.appoin.service.impl;

import cn.zn.modules.appoin.dao.AtrSpecialDao;
import cn.zn.modules.appoin.entity.AtrSpecialEntity;
import cn.zn.modules.appoin.entity.AtrSpecialWorkTime;
import cn.zn.modules.appoin.service.AtrSpecialService;
import cn.zn.modules.appoin.service.AtrSpecialWorkTimeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;

@Service
public class AtrSpecialServiceImpl extends ServiceImpl<AtrSpecialDao, AtrSpecialEntity> implements AtrSpecialService {

    @Autowired
    AtrSpecialWorkTimeService atrSpecialWorkTimeService;

    @Override
    public void saveSpecial(AtrSpecialEntity atrSpecialEntity) {
        super.save(atrSpecialEntity);
        // 保存工作时间
        if(atrSpecialEntity.getSpecialWorkTimes() != null){
            atrSpecialEntity.getSpecialWorkTimes().forEach(o -> {
                o.setSpecialId(atrSpecialEntity.getId());
                atrSpecialWorkTimeService.save(o);
            });
        }
    }

    @Override
    public void updateSpecial(AtrSpecialEntity atrSpecialEntity) {
        super.updateById(atrSpecialEntity);
        // 删除工作时间
        atrSpecialWorkTimeService.remove(new QueryWrapper<AtrSpecialWorkTime>().eq("SPECIAL_ID_", atrSpecialEntity.getId()));
        // 保存工作时间
        if(atrSpecialEntity.getSpecialWorkTimes() != null){
            atrSpecialEntity.getSpecialWorkTimes().forEach(o -> {
                o.setSpecialId(atrSpecialEntity.getId());
                atrSpecialWorkTimeService.save(o);
            });
        }
    }

    @Override
    public AtrSpecialEntity queryByDate(String ruleId, Date specialDate) {
        return this.baseMapper.queryByDate(ruleId, specialDate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        atrSpecialWorkTimeService.remove(new QueryWrapper<AtrSpecialWorkTime>().in("SPECIAL_ID_", idList));
        super.removeByIds(idList);
        return true;
    }
}
